<?php

namespace app\models;

use yii\db\ActiveRecord;

class Card extends ActiveRecord
{


    /**
     * 判断卡号是否存在
     * @param $cardNum
     * @return bool
     */
    public static function exists($cardNum)
    {
        return self::find()
            ->where(array(
                'card_num' => $cardNum
            ))
            ->exists();
    }

    /**
     * 添加
     * @param $cardNum
     * @return int
     */
    public static function add($cardNum)
    {
        return self::getDb()
            ->createCommand()
            ->insert(self::tableName(), array(
                'card_num' => $cardNum
            ))
            ->execute();
    }

    /**
     * 分页搜索
     * @param $page
     * @param $pageSize
     * @return array
     */
    public static function search($page, $pageSize)
    {
        $query = self::find();

        $queryTotal = clone $query;

        $offset = ($page - 1) * $pageSize;
        $query->orderBy('id desc')
            ->offset($offset)
            ->limit($pageSize);

        $total = $queryTotal->count('id');
        $rows = $query->asArray()
            ->all();

        return [$rows, $total];
    }

}